/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.editor.field;

import java.io.Serializable;
import java.util.Set;

import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.labelprovider.DkAsSimpleStringLabelProvider;
import com.daikit.daikit4gxt.client.ui.component.DkSpacer;
import com.daikit.daikit4gxt.client.ui.edit.ReferenceSelectorEvent;
import com.daikit.daikit4gxt.client.ui.edit.ReferenceSelectorEvent.HasReferenceSelectorHandlers;
import com.daikit.daikit4gxt.client.ui.edit.ReferenceSelectorEvent.ReferenceSelectorHandler;
import com.daikit.daikit4gxt.client.ui.editor.advanced.AbstractDkHideableAdapterField;
import com.daikit.daikit4gxt.client.ui.popup.DkReferenceSelectorPopup;
import com.daikit.daikit4gxt.client.ui.popup.DkReferenceSelectorPopup.MyReferenceSelectorPopupListener;
import com.google.gwt.event.shared.HandlerRegistration;
import com.sencha.gxt.cell.core.client.ButtonCell.ButtonScale;
import com.sencha.gxt.data.shared.Converter;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer.HorizontalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.TextField;


/**
 * A reference selector. Field with right button allowing to open a reference selector popup
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <T>
 */
public abstract class DkReferenceSelector<T extends Serializable> extends AbstractDkHideableAdapterField<T> implements
		Converter<T, String>, MyReferenceSelectorPopupListener<T>, HasReferenceSelectorHandlers<T>
{

	@Ignore
	private TextField textField;
	@Ignore
	private TextButton button;
	private LabelProvider<T> labelProvider;
	private boolean enabled = true;
	private T value;

	/**
	 * Constructor
	 */
	public DkReferenceSelector()
	{
		this(35);
	}

	/**
	 * Constructor with custom buttonWidth
	 * 
	 * @param buttonWidth
	 */
	public DkReferenceSelector(final int buttonWidth)
	{
		super(null);
		final HorizontalLayoutContainer panel = new HorizontalLayoutContainer();
		textField = new TextField();
		textField.setEmptyText(DkBaseMain.i18n().label_reference_selector_empty_text());
		button = new TextButton(DkBaseMain.i18n().label_reference_selector(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				onButtonClicked(value);
			}
		});
		button.setScale(ButtonScale.NONE);
		panel.add(textField, new HorizontalLayoutData(1, -1));
		panel.add(new DkSpacer(0), new HorizontalLayoutData(5, -1));
		panel.add(button, new HorizontalLayoutData(buttonWidth, 18));
		setWidget(panel);

		labelProvider = new DkAsSimpleStringLabelProvider<T>();
	}

	@Override
	public T getValue()
	{
		return value;
	}

	@Override
	public void setValue(final T value)
	{
		this.value = value;
		textField.setValue(convertModelValue(value));
		onValuSet(value);
		fireEvent(new ReferenceSelectorEvent<T>(value));
	}

	/**
	 * Should not be used
	 */
	@Deprecated
	@Override
	public T convertFieldValue(final String object)
	{
		return value;
	}

	protected void onButtonClicked(final T currentValue)
	{
		final DkReferenceSelectorPopup<T> referenceSelectorPopup = getReferenceSelectorPopup();
		if (referenceSelectorPopup != null)
		{
			referenceSelectorPopup.setSelectionMultiple(false);
			referenceSelectorPopup.show(this);
		}
	}

	@Override
	public String convertModelValue(final T value)
	{
		return value == null ? null : labelProvider.getLabel(value);
	};

	protected abstract DkReferenceSelectorPopup<T> getReferenceSelectorPopup();

	protected void onValuSet(final T value)
	{
		// Nothing done by default
	}

	@Override
	public void setEnabled(final boolean enabled)
	{
		textField.setEnabled(enabled);
		button.setEnabled(enabled);
		this.enabled = enabled;
	}

	@Override
	public boolean isEnabled()
	{
		return enabled;
	}

	/**
	 * Set the button Text
	 * 
	 * @param buttonText
	 *           the new button text
	 */
	public void setButtonText(final String buttonText)
	{
		button.setText(buttonText);
	}

	@Override
	public void onSelectionOk(final Set<T> selectedItems)
	{
		if (!selectedItems.isEmpty())
		{
			this.setValue(selectedItems.iterator().next());
		}
	}

	/**
	 * @return the labelProvider
	 */
	public LabelProvider<T> getLabelProvider()
	{
		return labelProvider;
	}

	/**
	 * @param labelProvider
	 *           the labelProvider to set
	 */
	public void setLabelProvider(final LabelProvider<T> labelProvider)
	{
		this.labelProvider = labelProvider;
	}

	/**
	 * Add an handler for selection
	 */
	@Override
	public HandlerRegistration addReferenceSelectorHandler(final ReferenceSelectorHandler<T> handler)
	{
		return addHandler(handler, ReferenceSelectorEvent.getType());
	}
}
